home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL08.TXT < prev    next >
Text File  |  1986-04-05  |  6KB  |  215 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 32
  2.  
  3.  
  4. TURBO-LESSON 8: CASE STATEMENT
  5.  
  6. OBJECTIVES - In lesson 8 you will learn about:
  7.  
  8. 1.  Block statements
  9. 2.  CASE statement
  10.  
  11.  
  12. 1.  Block statement.
  13.  
  14. As noted in an earlier lesson, the form of the IF statement is:
  15.  
  16. IF condition THEN statement_1 ELSE statement_2;
  17.  
  18. IF the condition is true, statement_1 is executed, otherwise 
  19. statement_2 is executed.  However, a single statement may not 
  20. always get the job done.  
  21.  
  22. The Block statement (also called a Compound statement) allows you 
  23. to substitute a multiple statement block anywhere a simple 
  24. statement is acceptable.  The form of the Block statement is:
  25.  
  26. BEGIN Statement_1; Statement_2 END;
  27.  
  28. You can include as many statements as you like between the BEGIN 
  29. and END.   
  30.  
  31. Notice that the main body of a Pascal program is a single 
  32. statement!  That single statement is a block statement:
  33.  
  34. BEGIN
  35.   Statement_1;
  36.   Statement_2;
  37.     .
  38.     .
  39.     .
  40.   Statement_n;
  41. END.
  42.  
  43. To illustrate the use of a Block statement in an IF statement, 
  44. consider the following problem:
  45.  
  46. If the value of I is greater than the value of J, swap the two in 
  47. memory (a common problem when sorting data).
  48.  
  49. If you happen to have a single statement to swap the values of 
  50. two memory locations, the IF statement might be:
  51.  
  52. IF I > J
  53.   THEN SWAP(I,J);
  54. î
  55. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 33
  56.  
  57.  
  58. Later, you will learn how to make up your own "statements" (by 
  59. creating functions and procedures), but for now, the way to swap 
  60. I and J is:
  61.  
  62. IF I > J
  63.   THEN
  64.     BEGIN
  65.       Temp := I;
  66.       I := J;
  67.       J := I;
  68.     END;
  69.  
  70. Notice that the form of this IF statement is still correct:
  71.  
  72. IF condition THEN statement;  
  73.  
  74. The statement, in this case, is a Block statement, rather than a 
  75. simple statement.  
  76.  
  77. ##### DO:
  78.  
  79. Take a look at the program called TEST1.
  80.  
  81. The sample programs which begin with the word TEST are provided 
  82. to make it quicker for you to test new statements and concepts.  
  83. The program, TEST1, has some integer variables and character 
  84. variables declared and the main BEGIN END.  All you need to do to 
  85. test a statement, or group of statements, is edit them into the 
  86. test program and run the program.
  87.  
  88. ##### DO:
  89.  
  90. Insert the following statements between the BEGIN and END of 
  91. program TEST1:
  92.  
  93. Write('Enter two numbers ');
  94. ReadLn(I, J);
  95. WriteLn('I=', I, ' J=', J);
  96.  
  97. Run the program.
  98.  
  99. ##### DO:
  100.  
  101. Between the ReadLn and WriteLn statements you just entered, add 
  102. the IF statement to swap I and J if I is larger:
  103.  
  104. IF I > J
  105.   THEN
  106.     BEGIN
  107.       Temp := I;
  108.       I    := J;
  109.       J    := Temp;
  110.     END;
  111.  
  112. Run the program several times using several pairs of input 
  113. numbers to test the program.  Does the "swap" work right?
  114. î
  115. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 34
  116.  
  117.  
  118. ##### DO:
  119.  
  120. Remove the BEGIN and END in the IF statement and run the program 
  121. several times.  
  122.  
  123. Does the "swap" still work right?
  124.  
  125. Without the BEGIN and END to make the three statements appear as 
  126. one,  the statements "appear" as follows to Pascal:
  127.  
  128. IF I > J
  129.   THEN
  130.     Temp := I;
  131. I := J;
  132. J := Temp;
  133.  
  134. Only the statement, Temp := I, is controlled by the IF condition.
  135.  
  136.  
  137. 2.  CASE statement.
  138.  
  139. First, a bit of review from an earlier lesson:
  140.  
  141. Program sequencing is done in Pascal with 
  142.  
  143.   (1) Simple Sequence,   one statement follows another,
  144.  
  145.   (2) Selection Structures,
  146.         IF  for one-way and two-way selection, 
  147.         CASE for many-way selection,
  148.  
  149.   (3) Repetition Structures,
  150.         REPEAT statement,
  151.         WHILE statement,
  152.         FOR statement.
  153.  
  154. The CASE statement is useful when there are more than two actions 
  155. or statement sequences needed.  The form of the CASE statement:
  156.  
  157. CASE variable OF
  158.     value_1  : Statement_1;
  159.     value_2  : Statement_2;
  160.       .
  161.       .
  162.       .
  163.     value_n  : Statement_n;
  164.   ELSE
  165.     Statement;
  166. END; {CASE}
  167. î
  168. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 35
  169.  
  170.  
  171. Note the following:
  172.  
  173. The variable must be a simple type such as Integer, CHAR, 
  174. BOOLEAN. (REAL is not allowed).
  175.  
  176. The values used to determine which Statement to execute must be 
  177. of the same type as the variable.
  178.  
  179. The values may be a constant, a list of constants, or a subrange 
  180. such as 1..10 (all integers from 1 to 10).
  181.  
  182. How it works:
  183.  
  184. If the variable has a value of "value_1" then Statement_1 is 
  185. executed.  If "value_2" then Statement_2, . . . 
  186.  
  187. If the value of the variable matches none of the values, the 
  188. statement following the ELSE is executed.
  189.  
  190. There must be an END to mark the end of the CASE statement. 
  191. (It's a good idea to add the comment {CASE} after the END).  
  192.  
  193. ##### DO:
  194.  
  195. Use the editor to examine PROG8.
  196.  
  197. This is the same problem as in the previous lesson, with a bit 
  198. more programming flexibility derived from the CASE statement and 
  199. the block statements.  
  200.  
  201. Notice that the list of variables in the CASE statement allow 
  202. appropriate responses for acceptable responses: A, a, B, b, C, c
  203.                              correct responses: D, d
  204.                         unacceptable responses: anything else.
  205.  
  206.  
  207. ##### DO:
  208.  
  209. Modify the CASE statement in PROG8 to accept C as the best 
  210. answer.
  211.  
  212. Run the program.  Did it work?
  213.  
  214. What message appears when D is entered?
  215. î